package health.lm.com.order.activity;

import android.os.Build;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.rxbus.RxBus;
import com.blankj.utilcode.util.SPUtils;
import com.lm.sjy.titlebar.widget.CommonTitleBar;

import butterknife.BindView;
import butterknife.OnClick;
import health.lm.com.component_base.base.event.BusProvider;
import health.lm.com.component_base.base.event.IBus;
import health.lm.com.component_base.base.mvp.activity.XActivity;
import health.lm.com.component_base.pay.PaymentEntity;
import health.lm.com.component_base.pay.alipay.AliPayHelper;
import health.lm.com.component_base.pay.wxpay.WxPayHelper;
import health.lm.com.order.R;
import health.lm.com.order.R2;
import health.lm.com.order.presenter.ActivationPresenter;
import health.lm.com.router.ARouterConstant;

/**
 * @author GQI
 * @description 激活
 * @date :2019-08-17 9:06
 */
@Route(path = ARouterConstant.ActivationActivity)
public class ActivationActivity extends XActivity<ActivationPresenter> {
    @BindView(R2.id.iv_weixin)
    ImageView mIvWeixin;
    @BindView(R2.id.cb_weixin)
    CheckBox mCbWeixin;
    @BindView(R2.id.iv_alipay)
    ImageView mIvAlipay;
    @BindView(R2.id.cb_alipay)
    CheckBox mCbAlipay;
    @BindView(R2.id.tv_pay)
    TextView mTvPay;
    @BindView(R2.id.title_bar)
    CommonTitleBar mTitleBar;
    @BindView(R2.id.tv_money)
    TextView tvMoney;
    @Autowired
    Bundle jumpBundle;
    @Autowired
    String from;
    @Autowired
    String activate_fee;

    /**
     * 支付方式 0、未选择 1、微信 2、支付宝
     */
    private int payment_type = 0;
    //订单ID
    private String mOrder_id;

    @Override
    public int getLayoutId() {
        return R.layout.activity_activation;
    }

    @Override
    public ActivationPresenter newP() {
        return new ActivationPresenter();
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        initWidget();
        getP().getData();
    }

    protected void initWidget() {
        mTitleBar.setListener((v, action, extra) -> finish());
        mCbWeixin.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                payment_type = 1;
                mCbAlipay.setChecked(false);
            }
            if (!isChecked && !mCbAlipay.isChecked()) {
                mCbWeixin.setChecked(true);
            }
        });
        mCbAlipay.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                payment_type = 2;
                mCbWeixin.setChecked(false);
            }
            if (!isChecked && !mCbWeixin.isChecked()) {
                mCbAlipay.setChecked(true);
            }
        });
        mCbWeixin.setChecked(true);

    }

    @Override
    public void bindEvent() {
        super.bindEvent();
        BusProvider.getBus().subscribe(context,  new RxBus.Callback<IBus.AbsEvent>() {
            @Override
            public void onEvent(IBus.AbsEvent absEvent) {
                if (absEvent.getTag() == 1){
                    aliPaySuccess();
                }else if(absEvent.getTag() == 0){
                    aliPayFail();
                }
            }
        });
    }

    @Override
    public boolean useEventBus() {
        return true;
    }

    /**
     * @param paymentEntity 支付参数
     */
    public void paymentData(PaymentEntity paymentEntity) {
        /*need_pay 1需要支付 0不需要支付 跳到支付成功*/
        //pay_type 1微信 2支付宝
        if(paymentEntity == null) {
            return;
        }
        SPUtils.getInstance().put("order_sn",paymentEntity.getOrder_sn());
        if (payment_type == 2) {
            AliPayHelper.getInstance().pay(mActivity, paymentEntity.getAli_pay_info(), payResult -> {
                getP().resultCheck(paymentEntity.getOrder_sn(), payment_type);
            });
        } else {
            WxPayHelper.getInstance().pay(mActivity, paymentEntity.getWeixin_pay_info());
        }
    }


    /**
     * 设置是否可以选择支付方式
     */
    private void setEnable(boolean enable) {
        mCbWeixin.setEnabled(enable);
        mCbAlipay.setEnabled(enable);
    }


    @OnClick(value = {R2.id.tv_pay})
    public void onClick(View v) {
        if (R.id.tv_pay == v.getId()) {
            getP().getReadyPay(activate_fee, payment_type);
        }
    }

    public void getDataSuccess() {
        //费用
        String price = "<font color=#000000>你需要支付</font><font color=#e1b65c> " + activate_fee + "</font><font color=#000000>元</font>";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            tvMoney.setText(Html.fromHtml(price, Html.FROM_HTML_MODE_COMPACT));
        } else {
            tvMoney.setText(Html.fromHtml(price));
        }
    }

    public void aliPaySuccess(){
        getvDelegate().toastShort("支付成功");
        finish();
    }
    public void aliPayFail(){
        getvDelegate().toastShort("支付失败请稍后再试");
    }
}
